package com.sora.sora.security.login.service.impl;

import org.springframework.stereotype.Service;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

@Service("imgCaptchaService")
public class CaptchaServiceImpl implements com.sora.sora.security.login.service.ImgCaptchaService {

    private final String[] fontFamily = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};
    private final Random random = new Random();
    private final int size = 18;


    @Override
    public Font generateFont() {
        String style = fontFamily[random.nextInt(fontFamily.length)];
        int bound = random.nextInt(4);
        int size = this.size + random.nextInt(10);
        return new Font(style, bound, size);
    }

    @Override
    public void drawLine(BufferedImage image) {
        Graphics graphics = image.getGraphics();

    }

    @Override
    public Color generateColor(int s, int e) {
        if (s < 0 || s > 255) {
            s = 120;
        }
        if (e < 0 || e > 255) {
            e = 150;
        }

        int r,g,b;
        r = s + random.nextInt(e-s);
        g = s + random.nextInt(e-s);
        b = s + random.nextInt(e-s);

        return new Color(r, g, b);
    }
}
